/**
 *@Description 物流线路配置
 *@Author chenxq
 *@Date 201516-01-08
 */
var routeCtrl  = new XFApp.client();
XFApp.client.prototype.route = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryByPageURL : basePath+"/logistics/route/queryPage",
        getRouteURL : basePath+"/logistics/route/detail",
        saveRouteURL : basePath+"/logistics/route/save",
        delRouteURL : basePath+"/logistics/route/delete",
        getReceiverListURL:basePath+"/logistics/receiver/getReceiverList"
    },
    queryPage : function(){
        _this = this;
        if(!!_this.page){
            _this.page.page("remote",0,$('#searchForm').serialize());
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                currentPageIndex : Number($("#currentPageIndex").val())-1,
                remote: {
                    url: _this.urlItems.queryByPageURL,
                    params: $('#searchForm').serialize(),
                    success: function (data, pageIndex) {
                        $("#currentPageIndex").val(data.result.pageNo);
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(null == data
                                || null == data.result
                                || null == data.result.itemList
                                || data.result.itemList.length < 1){
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }else{
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }
                        }else{
                            webDialogUtil.simpleAlert("数据查询失败!");
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询失败!");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    formValidate : function(){
        $("#editRouteForm").validate({
            rules: {
                dayMin: {
                    required: true,
                    number:true
                },
                dayMax: {
                    required: true,
                    number:true
                },
                routeName: "required",
                concatName: "required",
                concatPhone: {
                    required:true
                },
                concatEmail: {
                    required: true,
                    email:true
                },
                address: "required",
                nationValidate: "required",
                nation1Validate: "required"
            },
            messages: {
                dayMin: {
                    required: "请填写最短时效",
                    number:"最短时效必须为数字"
                },
                dayMax: {
                    required: "请填写最长时效",
                    number:"最c长时效必须为数字"
                },
                routeName: "请填写线路名称",
                concatName: "请填写联系人姓名",
                concatPhone:  {
                    required : "请填写联系电话"
                },
                concatEmail: {
                    required : "请填写联系邮箱",
                    email : "邮箱格式错误"
                },
                address: "请填写公司地址",
                nationValidate: "请选择出口国",
                nation1Validate: "请选择目的国"
            }
        });
        $("#editRouteForm").validate();
    },
    editRouteView : function(id,typeId){
        _this = this;
        if(null == id){
            return this.editRouteDialog(null,typeId);
        }
        $.ajax({
            url:_this.urlItems.getRouteURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(false == data.isSuccess){
                    window.webDialogUtil.alert("提示","获取数据失败");
                    return;
                }
                _this.editRouteDialog(data.result,typeId);
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    editRouteDialog : function(data,typeId){
        var _this = this;
        //国家下拉框
        dictCtrl.dictionary.load("nation","nationHidden");
        dictCtrl.dictionary.load("nation1","nation1Hidden");
        if(null != data){
            $("#editRouteForm #id").val(data.id);
            $("#editRouteForm #routeName").val(data.routeName);
            $("#editRouteForm #firstWeight").val(data.firstWeight);
            $("#editRouteForm #firstFreight").val(data.firstFreight);
            $("#editRouteForm #continueWeight").val(data.continueWeight);
            $("#editRouteForm #continueFreight").val(data.continueFreight);
            $("#editRouteForm #commonWeight").val(data.commonWeight);
            $("#editRouteForm #commonFreight").val(data.commonFreight);
            $("#editRouteForm #goldRebate").val((data.goldRebate*10).toFixed(2));
            $("#editRouteForm #silverRebate").val((data.silverRebate*10).toFixed(2));
            $("#editRouteForm #nationHidden").val(data.shipperCty);
            $("#editRouteForm #nation1Hidden").val(data.receiverCty);
            $("#editRouteForm #updateTime").text(data.updateTime);
            $("#editRouteForm #updatePerson").text(data.updatePerson);
            $("#editRouteForm #logisticsId").val(data.logisticsId)
            var limitTime = data.limitTime;
            if(limitTime){
                $("#editRouteForm #dayMin").val(limitTime.split("-")[0]);
                $("#editRouteForm #dayMax").val(limitTime.split("-")[1]);
            }
            var receiverId = data.receiverId;
            $.ajax({
                url:_this.urlItems.getReceiverListURL,
                data:{logisticsId:data.logisticsId},
                type:'post',
                cache:false,
                dataType:'json',
                success:function(data) {
                    if(true == data.isSuccess){
                        var strs = new Array();
                        var result = data.result;
                        for(var i = 0; i < result.length; i++){
                            var address = result[i].receiverArea + " " + result[i].receiverAddress;
                            var addHtml = "";
                            if(receiverId == result[i].id){
                                addHtml = "<p><input name='receiver' type='radio' checked='checked' value='"+result[i].id+"'/>" + address + "</p>";
                            }else{
                                addHtml = "<p><input name='receiver' type='radio' value='"+result[i].id+"'/>" + address + "</p>";
                            }
                            strs.push(addHtml);
                        }
                        $("#receiverList").html(strs.join(""));
                    }
                },
                error : function() {
                    webDialogUtil.simpleAlert("操作失败!");
                }
            });
        }else{
            $("#editRouteForm #id").val("");
            $("#editRouteForm #logisticsName").val("");
            $("#editRouteForm #logisticsId").val("");
            $("#editRouteForm #routeName").val("");
            $("#editRouteForm #firstWeight").val("");
            $("#editRouteForm #firstFreight").val("");
            $("#editRouteForm #continueWeight").val("");
            $("#editRouteForm #continueFreight").val("");
            $("#editRouteForm #commonWeight").val("");
            $("#editRouteForm #commonFreight").val("");
            $("#editRouteForm #goldRebate").val("");
            $("#editRouteForm #silverRebate").val("");
            $("#editRouteForm #receiverId").val("");
            $("#editRouteForm #updateTime").text("");
            $("#editRouteForm #updatePerson").text("");
        }
        webAlert({
            title:routeCtrl.route.titleDisplay(typeId),
            content:document.getElementById("editView"),
            button:[
                {
                    name:"保存",
                    callback:function(){
                        var logisticsId = $("#logisticsId").val();
                        if(logisticsId == '' || logisticsId == null){
                            $("#logisticsId-error").text("请选择物流公司");
                            return false;
                        }
                        if($("#editRouteForm").validate().form()){
                            routeCtrl.route.saveRouteForm();
                        }else{
                            return false;
                        }
                    }},
                {
                    name:"关闭",
                    callback:function(){
                        this.close();
                    }
                }]
        });
    },
    saveRouteForm : function(){
        _this = this;
        $("#limitTime").val($("#dayMin").val() + "-" + $("#dayMax").val());
        var receiverId =  $('input:radio[name="receiver"]:checked').val();
        $("#editRouteForm #receiverId").val(receiverId)
        var logisticsName = $("#editRouteForm #logisticsId").find("option:selected").text();
        $("#editRouteForm #logisticsName").val(logisticsName);
        $.ajax( {
            url:_this.urlItems.saveRouteURL,
            data:$('#editRouteForm').serialize(),
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                window.webDialogUtil.alert("提示",data.msg);
                routeCtrl.route.queryPage();
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    titleDisplay : function(typeId){
        switch(typeId){
            case 0:return "查看";
            case 1:return "修改";
            case 2:return "新增";
            default : return "";
        }
    },
    delRouteDialog : function(id){
        _this = this;
        webDialogUtil.simpleConfrimAlert("确定要删除此运单记录吗?", function(){routeCtrl.route.delRoute(id);});
    },
    delRoute : function(id){
        _this = this;
        $.ajax({
            url:_this.urlItems.delRouteURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(true == data.isSuccess){
                    webDialogUtil.simpleAlert(data.msg);
                    routeCtrl.route.queryPage();
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    getReceiverList: function(logisticsId){
        var _this = this;
        $.ajax({
            url:_this.urlItems.getReceiverListURL,
            data:{logisticsId:logisticsId},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(true == data.isSuccess){
                    var strs = new Array();
                    var result = data.result;
                    for(var i = 0; i < result.length; i++){
                        var address = result[i].receiverArea + " " + result[i].receiverAddress;
                        var addHtml = "";
                        if(result[i].isDefault){
                            addHtml = "<p><input name='receiver' type='radio' checked='checked' value='"+result[i].id+"'/>" + address + "</p>";
                        }else{
                            addHtml = "<p><input name='receiver' type='radio' value='"+result[i].id+"'/>" + address + "</p>";
                        }
                        strs.push(addHtml);
                    }
                    $("#receiverList").html(strs.join(""));
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    event : function(){
        _this = this;
        $("#editRouteForm #logisticsId").on("change",function(){
            var logisticsId = $(this).find("option:selected").val();
            if(logisticsId != ''){
                $("#editRouteForm #logisticsName").val(logisticsName);
                routeCtrl.route.getReceiverList(logisticsId);
            }
        })
        $("#searchButton").on("click",function(){
            routeCtrl.route.queryPage();
        });
        routeCtrl.route.queryPage();
        this.formValidate();
    }
};
$.views.converters({

});
$(function(){
    routeCtrl.route.event();
});